//@author A0096466N
//
// **********
//
// clash class takes in the currently displayed tasks
// and checks if there are any timing clashes
//
//
// **********

#include "Clash.h"

Clash::Clash(void) {

}

Clash::~Clash(void) {
}

time_t Clash::getTimeInSeconds(string time, time_t seconds) {

	assert(time.size() == 4);

	struct tm * eventTime = localtime(&seconds); 

	// converting string time to structTM time
	(*eventTime).tm_hour = atoi(time.substr(0,2).c_str());  
	(*eventTime).tm_min = atoi(time.substr(2,2).c_str());
	time_t eventSeconds = mktime(eventTime);									
					
	return eventSeconds;
}

string Clash::getStartTime(string range) {
	string startTime = range.substr(0,4);
	return startTime;
}

string Clash::getEndTime(string range) {
	int find = range.find_first_of(DASH);
	int find1 = range.find_first_not_of(WHITE_SPACE,find+1);
	string endTime = range.substr(find1,4);

	return endTime;
}

string Clash::execute(vector<Task> store) {
	ofstream writeFile;
	writeFile.open(FILENAME_LOGGER.c_str(), ofstream::out | ofstream::app);
	writeFile << "Attempting to identify timing clash" << endl;
	writeFile.close();


	// less than 2 task impossible to clash
	if(store.size() < 2) {
		return EMPTY;
	}

	for(unsigned int i = 0; i < store.size()-1; i++) {
		for(unsigned int j = i+1; j < store.size(); j++) {

			//check only if same date, use dateInSeconds because deadline only stores day/month may be different years.
			if(store[i].getDateInSeconds() == store[j].getDateInSeconds()) {
				int found1 = store[i].getTime().find(DASH);
				int found2 = store[j].getTime().find(DASH);

				//check only if it both tasks have interval timings, denoted by "-" (DASH)
				if(found1 != string::npos && found2 != string::npos) {
					
					//each string should contain 4 digits representing 24h time format
					string event1StartString = getStartTime(store[i].getTime());
					string event1EndString = getEndTime(store[i].getTime());
					string event2StartString = getStartTime(store[j].getTime());
					string event2EndString = getEndTime(store[j].getTime());
								

					time_t time = (store[i]).getDateInSeconds();						 
					struct tm * eventTime = localtime(&time); 

					time_t event1Start = getTimeInSeconds(event1StartString,time);
					time_t event1End = getTimeInSeconds(event1EndString,time);
					time_t event2Start = getTimeInSeconds(event2StartString,time);
					time_t event2End = getTimeInSeconds(event2EndString,time);

					if(event1Start <= event2Start && event1End > event2Start) {			 
						string feedback = CLASH_FEEDBACK1 + to_string(i+1);
						feedback += (CLASH_FEEDBACK2 + to_string(j+1));
						return feedback;
					}

					if(event2Start <= event1Start && event2End > event1Start) {			
						string feedback = CLASH_FEEDBACK1 + to_string(i+1);
						feedback += (CLASH_FEEDBACK2 + to_string(j+1));
						return feedback;
					}
				}
		
			}
		}
	}
	return EMPTY;
}
